package com.ats.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ats.model.User;
import com.ats.service.JobRecordSerivce_xh;
import com.ats.service.JobStateService_xh;
import com.ats.service.UserSerivce_xh;
import com.ats.serviceimpl.JobRecordSerivceImpl_xh;
import com.ats.serviceimpl.JobStateServiceImpl_xh;
import com.ats.serviceimpl.UserServiceImpl_xh;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 通过状态查询作业记录
 * @author xh
 *
 */
public class FindJobRecordByStateController_xh extends HttpServlet {	
	UserSerivce_xh us = new UserServiceImpl_xh();
	JobStateService_xh js = new JobStateServiceImpl_xh();
	JobRecordSerivce_xh jrs =new JobRecordSerivceImpl_xh();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setCharacterEncoding("utf8");
		HttpSession session =req.getSession();
		PrintWriter  pw = resp.getWriter();
		int jsid =Integer.parseInt(req.getParameter("jsid"));
		int uid =Integer.parseInt(req.getParameter("uid"));
		//获取用户权限
		int ustate =us.getUstateByUid(uid);
		if(ustate==1){
			//老师对应的作业信息回写页面
			int tid =((User)session.getAttribute("user")).getTid();
			JSONArray jso=jrs.findAllJobRecordByTidAndJsid(tid, jsid);
			pw.write(jso.toString());
		}else if(ustate==2){
			//学生对应的作业信息回写页面
			int sid =((User)session.getAttribute("user")).getSid();
			JSONArray jso =jrs.findAllJobRecordBySidAndJsid(sid, jsid);
			JSONObject json =new JSONObject();
			pw.write(jso.toString());
		}
		pw.flush();
		pw.close();	
		
	}

}
